package com.yanqu.road.server.manger;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.seacraft.config.SeacraftSouthSeaActivityConfig;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.utils.date.DateHelper;

/**
 * @Description 海上争霸 南海丝路 通用类
 * @Author cwq
 * @Data 2022/1/21 16:51
 */
public class SeacraftSouthSeaCommonMgr {

    /**
     * 是否是捐献期
     *
     * @return
     */
    public static boolean isPrepareTime(ActivityInfo activityInfo, SeacraftSouthSeaActivityConfig seaActivityConfig) {
        if(isPrepareTimeRound1(activityInfo,seaActivityConfig)){
            return true;
        }
        int now = DateHelper.getCurrentSecond();
        for (int i = 0; i < seaActivityConfig.roundTimeList.size() - 1; i++) {
            // 比赛期间的捐献期
            long endTimeRound1 = activityInfo.getBeginTime() + seaActivityConfig.roundTimeList.get(i)[1];
            long startTimeRound2 = activityInfo.getBeginTime() + seaActivityConfig.roundTimeList.get(i+1)[0];
            if(endTimeRound1 <= now && now < startTimeRound2){
                return true;
            }
        }
        return false;
    }

    /**
     * 南海丝路 第一回合的备战期
     *
     * @return
     */
    public static boolean isPrepareTimeRound1(ActivityInfo activityInfo, SeacraftSouthSeaActivityConfig seaActivityConfig) {
        if(activityInfo == null){
            return false;
        }
        if(seaActivityConfig == null){
            return false;
        }
        if(!ActivityHelper.activityInTime(activityInfo)){
            return false;
        }
        int now = DateHelper.getCurrentSecond();
        if(activityInfo.getBeginTime() + seaActivityConfig.pretimeList.get(0) <= now && now < activityInfo.getBeginTime() + seaActivityConfig.pretimeList.get(1)){
            return true;
        }
        return false;
    }


    /**
     * 南海丝路 是否开启
     *
     * @return
     */
    public static boolean isOpenTime(ActivityInfo activityInfo, SeacraftSouthSeaActivityConfig seaActivityConfig) {
        if (!ActivityHelper.activityInShowTime(activityInfo)) {
            return false;
        }
        int curRound = getCurRound(activityInfo, seaActivityConfig);
        if (curRound == 0 || seaActivityConfig.roundTimeList.size() < curRound) {
            return false;
        }
        Integer[] config = seaActivityConfig.roundTimeList.get(curRound - 1);
        long openTime = config[0] + activityInfo.getBeginTime();
        long endTime = config[1] + activityInfo.getBeginTime();
        int now = DateHelper.getCurrentSecond();
        if (now >= openTime && now < endTime) {
            return true;
        }
        return false;
    }


    /**
     * 获取当前比赛的轮次
     * 第一轮从海盗之 结算开始 1 海盗之乱结束-第一阶段结束 2 第一阶段结束 - 第二阶段结束 3第二阶段结束
     * <p>
     * 从1开始 没有 返回0
     *
     * @return
     */
    public static int getCurRound(ActivityInfo activityInfo, SeacraftSouthSeaActivityConfig seaActivityConfig) {
        int now = DateHelper.getCurrentSecond();
        int round = 0;

        long haiDaoEndTime = activityInfo.getBeginTime() + seaActivityConfig.haiDaoEndTime;
        if (now >= haiDaoEndTime) {
            round++;
        }

        for (Integer[] config : seaActivityConfig.roundTimeList) {
            long endTime = config[1] + activityInfo.getBeginTime();
            if (now >= endTime) {
                round++;
            }
        }
//        for (int i = 0; i < seaActivityConfig.roundTimeList.size(); i++) {
//            Integer[] config = seaActivityConfig.roundTimeList.get(i);
//            long openTime = config[0] + activityInfo.getBeginTime();
//            long endTime = config[1] + activityInfo.getBeginTime();
//            if(now >= openTime && now < endTime){
//                return i + 1;
//            }
//        }
        return round;
    }

    public static void builderSeacraftSouthSeaPatronsEntity() {

    }

    public static int comparator(int patronsId1, long earn1, long addPlus1, int patronsId2, long earn2, long addPlus2) {
        long value1 = earn1 * (1000 + addPlus1) / 1000;
        long value2 = earn2 * (1000 + addPlus2) / 1000;
        if (value1 == value2) {
            return Long.compare(patronsId2, patronsId1);
        }
        return Long.compare(value2, value1);
    }


    /**
     * 本轮晋级人数
     * @return
     */
    public static int thisRoundPromotionNum(int round ,SeacraftSouthSeaActivityConfig activityConfig){
        int promotionNum = 0;
        if (activityConfig != null && round > 0) {
            if (round <= activityConfig.roundTimeList.size()) {
                Integer[] config = activityConfig.roundTimeList.get(round - 1);
                promotionNum = config[2];
            } else {
                // 最后一回合默认 晋级一个
                promotionNum = 1;
            }
        }
        return promotionNum;
    }

}




